package com.wtx.fitness.service.impl;

import com.wtx.fitness.entity.Announcement;
import com.wtx.fitness.mapper.AnnouncementMapper;
import com.wtx.fitness.service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 公告服务实现类
 */
@Service
public class AnnouncementServiceImpl implements AnnouncementService {

  @Autowired
  private AnnouncementMapper announcementMapper;

  @Override
  public List<Announcement> getAllAnnouncements() {
    return announcementMapper.selectAll();
  }

  @Override
  public List<Announcement> getAnnouncementsByPage(int pageNum, int pageSize) {
    int offset = (pageNum - 1) * pageSize;
    return announcementMapper.selectByPage(offset, pageSize);
  }

  @Override
  public Announcement getAnnouncementById(Integer id) {
    return announcementMapper.selectById(id);
  }

  @Override
  public int addAnnouncement(Announcement announcement) {
    // 设置发布时间和更新时间
    Date now = new Date();
    announcement.setPublishTime(now);
    announcement.setUpdateTime(now);

    return announcementMapper.insert(announcement);
  }

  @Override
  public int updateAnnouncement(Announcement announcement) {
    // 更新时间设置为当前时间
    announcement.setUpdateTime(new Date());

    return announcementMapper.update(announcement);
  }

  @Override
  public int deleteAnnouncement(Integer id) {
    return announcementMapper.deleteById(id);
  }
}